package com.upu173.music.fragment;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.andbase.library.app.base.AbBaseFragment;
import com.andbase.library.utils.AbLogUtil;
import com.andbase.library.utils.AbViewUtil;
import com.hw.lrcviewlib.ILrcViewSeekListener;
import com.hw.lrcviewlib.LrcDataBuilder;
import com.hw.lrcviewlib.LrcRow;
import com.hw.lrcviewlib.LrcView;
import com.upu173.music.R;
import com.upu173.music.activity.MusicPlayActivity;
import com.upu173.music.global.PlayerData;
import com.upu173.music.service.MusicPlayerService;
import com.upu173.music.utils.MusicUtil;

import java.io.File;
import java.util.List;


public class LrcViewFragment extends AbBaseFragment {

    private MusicPlayActivity activity;
    private LrcView lrcView;
    private View rootView = null;
    private String currentPlayPath;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.music_view_lrc, container, false);

        activity = (MusicPlayActivity)this.getActivity();

        lrcView = (LrcView)rootView.findViewById(R.id.lrc_view);

        //init the lrcView
        lrcView.getLrcSetting()
                .setTimeTextSize(40)//时间字体大小
                .setSelectLineColor(Color.parseColor("#FFFFFF"))//选中线颜色
                .setSelectLineTextSize(25)//选中线大小
                .setHeightRowColor(Color.parseColor("#31C27C"))//高亮字体颜色
                .setNormalRowTextSize((int)AbViewUtil.sp2px(activity, 17.0f))//正常行字体大小
                .setHeightLightRowTextSize((int)AbViewUtil.sp2px(activity, 17.0f))//高亮行字体大小
                .setTrySelectRowTextSize((int)AbViewUtil.sp2px(activity, 17.0f))//尝试选中行字体大小
                .setTimeTextColor(Color.parseColor("#FFFFFF"))//时间字体颜色
                .setTrySelectRowColor(Color.parseColor("#55FFFFFF"))////尝试选中字体颜色
                .setMessagePaintTextSize((int)AbViewUtil.sp2px(activity, 14.0f))//正常行字体大小
        ;


        lrcView.commitLrcSettings();


        lrcView.setLrcViewSeekListener(new ILrcViewSeekListener() {
            @Override
            public void onSeek(LrcRow currentLrcRow, long CurrentSelectedRowTime) {
                //在这里执行播放器控制器控制播放器跳转到指定时间
                //mController.seekTo((int) CurrentSelectedRowTime);
            }
        });

        //播放器播放时，时间更新后调用这个时歌词数据更新到当前对应的歌词，播放器一般时间更新以秒为频率更新
        //lrcView.smoothScrollToTime(time)//传递的数据是播放器的时间格式转化为long数据

        return rootView;
    }

    public void checkCurrentPlay(){

        if(!PlayerData.getCurrentPlaySong().getPath().equals(currentPlayPath)){
            loadData();
            currentPlayPath = PlayerData.getCurrentPlaySong().getPath();
        }

        int currentTime = MusicPlayerService.playerManagerReceiver.getCurrentPosition();
        if(lrcView!=null){
            lrcView.smoothScrollToTime(currentTime);
        }
    }

    public void loadData(){
        if(PlayerData.getCurrentPlaySong()!=null){
            AbLogUtil.e("LrcViewFragment","LrcViewFragment loadData:" + PlayerData.getCurrentPlaySong().getName());
            //List<LrcRow> lrcRows = new LrcDataBuilder().BuiltFromAssets(this, songPath.substring(0,songPath.lastIndexOf(".")) + ".lrc");
            List<LrcRow> lrcRows = new LrcDataBuilder().Build(new File(PlayerData.getCurrentPlaySong().getPath().substring(0,PlayerData.getCurrentPlaySong().getPath().lastIndexOf(".")) + ".lrc"));
            //lrcView.setTextSizeAutomaticMode(true);//是否自动适配文字大小
            lrcView.setLrcData(lrcRows);
        }

    }

    @Override
    public void onResume() {
        checkCurrentPlay();
        super.onResume();
    }

}
